package com.login.json;

import com.login.domain.details.AdminUserDetails;
import com.login.domain.details.WebUserDetails;
import com.login.domain.dto.LoginDTO;
import org.springframework.security.authentication.AbstractAuthenticationToken;

/**
 * @author 路亚
 * @version 0.1
 * @className Json
 * @description
 * @date 2024/12/12 16:25
 * @since jdk11
 */
public class JsonAuthenticationToken extends AbstractAuthenticationToken {

    private String username;
    private String password;
    private Integer type;

    private Object credentials;
    private Object principal;

    public JsonAuthenticationToken(LoginDTO loginDTO) {
        super(null);
        this.username = loginDTO.getUsername();
        this.password = loginDTO.getPassword();
        this.type = loginDTO.getType();
        this.setAuthenticated(false);
    }

    public JsonAuthenticationToken(Object obj) {
        super(null);
        if (obj instanceof AdminUserDetails){
            AdminUserDetails adminUserDetails = (AdminUserDetails) obj;
            this.principal = adminUserDetails;
            this.credentials = adminUserDetails.getPassword();
        }else if (obj instanceof WebUserDetails){
            WebUserDetails webUserDetails = (WebUserDetails) obj;
            this.principal = webUserDetails;
            this.credentials = webUserDetails.getPassword();
        }
        this.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public Integer getType() {
        return type;
    }

}
